import { BrowserWindow } from 'electron'
import { join } from 'path'
import icon from '../../../resources/icon.png?asset'
import { is } from '@electron-toolkit/utils'

export class WindowManager {
  windowView: BrowserWindow
  constructor(options = {}) {
    this.windowView = new BrowserWindow({
      width: 1000,
      height: 500,
      show: false,
      autoHideMenuBar: true,
      ...(process.platform === 'linux' ? { icon } : {}),
      webPreferences: {
        preload: join(__dirname, '../preload/index.js'),
        sandbox: false
      },
      ...options
    })
  }
  // 显示
  showWindow(route) {
    if (is.dev && process.env['ELECTRON_RENDERER_URL']) {
      this.windowView.loadURL(`${process.env['ELECTRON_RENDERER_URL']}/#${route}`)
      console.log('electron-vite child window as start at:', process.env['ELECTRON_RENDERER_URL'])
    } else {
      this.windowView.loadFile(join(__dirname, '../renderer/index.html/#${route}'))
      console.log(
        'electron-vite child window as start at:',
        join(__dirname, '../renderer/index.html/#${route}')
      )
    }
    // 显示窗口
    this.windowView.show()
    this.windowView.webContents.openDevTools()
    return this.windowView
  }
}
